#!/usr/local/bin/php
<?php
require_once('connections/mysqlDB.php');
require('smarty_connect.php');

$smarty = new smarty_connect;
$dbConn = new mysqli(HOST,USER,PWD,DB);

// To protect MySQL injection (more detail about MySQL injection)

/* prepare statement */
if ($stmt = $dbConn->query("SELECT create_date, total, today FROM status WHERE create_date = FROM_DAYS( TO_DAYS( now() ) -2 ) ")) {
	while ($row = $stmt->fetch_assoc()) {
	$prev_total = $row["total"];
	$prev_today= $row["today"];
	}
}
$stmt->close();

if ($stmt = $dbConn->query("select count(*) as total from registration_info")) {
	while ($row = $stmt->fetch_assoc()) {
	$total = $row["total"];
	}
}
$stmt->close();

if ($stmt = $dbConn->query("select count(*) as today from registration_info where date(create_date) = FROM_DAYS( TO_DAYS( now() ) -1 ) ")) {
	while ($row = $stmt->fetch_assoc()) {
	$today = $row["today"];
	}	
}
$stmt->close();
$diff=$total-$today-$prev_total;

 if ($stmt2 = $dbConn->prepare("INSERT INTO status (total, today, diff,create_date)  VALUES (?,?,?,FROM_DAYS( TO_DAYS( now() ) -1 ) )")) {
    $stmt2->bind_param('iii', $total, $today, $diff);
    $stmt2->execute();
    //printf ("<br>Added records: %d<br>", $stmt2->affected_rows);
  }
$stmt2->close();

$stmt1 = $dbConn->query("select date_format(create_date, '%m/%d/%y') as create_date, total, today, diff from status where create_date = FROM_DAYS( TO_DAYS( now() ) -1 ) ");
$data=array();
while ($row = $stmt1->fetch_assoc()) {
$date= $row['create_date'];
$total= $row['total'];
$today= $row['today'];
$diff= $row['diff'];
};
$stmt1->close();
$message ="<table width=\"100%\"><tr><th width=\"20%\">Date</th><th width=\"20%\">Total</th><th>Today</th><th>Difference</th><tr>";
$message=$message . "<tr><td>$date</td><td>$total</td><td>$today</td><td>$diff</td></tr></table>";
//echo $message;

$to="cyang255@yahoo.com, jennylan2000@yahoo.com, haihong_frcc@yahoo.com, stevenfrcc@gmail.com, ellisaliu@yahoo.com";
$subject = "慕主特會報名";
$message = "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
</head>
<body>
$head
$message
</body>
</html>";

// Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";// More headers
$headers .= 'From:FRCC Admin<support@frccinnerlife.org>' . "\r\n";
$headers .= "\r\n";


$mail = mail($to,$subject,$message,$headers);

?>
